﻿using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.MediaCenter;
using Microsoft.MediaCenter.Hosting;
using Microsoft.MediaCenter.UI;
using MusicBrowser.LibraryManagement;
using MusicBrowser.Util;
using System.Text;
using System;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading;
using System.IO;
using System.Collections;


namespace MusicBrowser.LibraryManagement
{
   

    public class Playlist
    {

        private StringBuilder pl;

        public void PlayAll(FolderItemListMCE item)
        {
            string path = item.folderItems.Path;
            pl = new StringBuilder("");
            Microsoft.MediaCenter.UI.Application.DeferredInvokeOnWorkerThread(CreatePlaylist, Done, path);
        }
       
        private void CreatePlaylist(object obj)
        {
            ThreadPriority priority = Thread.CurrentThread.Priority;
            Thread.CurrentThread.Priority = ThreadPriority.Lowest;

            string path = Convert.ToString(obj);

            try
            {

                foreach (string fileName in Directory.GetFiles(path, "*.*"))
                {
                    if (Helper.IsSong(fileName))
                    {

                        pl.AppendLine(fileName);
                    }

                }
                // Add all directories at this directory.
                string[] subdirEntries = Directory.GetDirectories(path);
                foreach (string directoryName in subdirEntries)
                {

                    try
                    {
                        DirectoryInfo info = new DirectoryInfo(directoryName);
                        if ((info.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
                            CreatePlaylist(directoryName);

                    }
                    catch (UnauthorizedAccessException) { }
                }


            }
            finally
            {
                //
                // Reset our thread's priority back to its previous value
                //
                Thread.CurrentThread.Priority = priority;
            }
            
        }

        private void Done(object obj)
        {
            if (pl.Length > 0)
            {
                string playlistPath = System.IO.Path.GetTempPath() + "playlist.m3u";
                System.IO.File.WriteAllText(playlistPath, pl.ToString(), Encoding.Default);
                MediaCenterEnvironment mce;
                mce = Microsoft.MediaCenter.Hosting.AddInHost.Current.MediaCenterEnvironment;
                mce.PlayMedia(Microsoft.MediaCenter.MediaType.Audio, playlistPath, false);
                mce.MediaExperience.GoToFullScreen();
                
            }
            else
            {
                Microsoft.MediaCenter.Hosting.AddInHost.Current.MediaCenterEnvironment.Dialog("No music file !", "Message", DialogButtons.Ok, 10, true);
            }
            
           
        }

        public void AddAll(FolderItemListMCE item)
        {

            if (Microsoft.MediaCenter.Hosting.AddInHost.Current.MediaCenterEnvironment.MediaExperience == null)
            {
                PlayAll(item);
                return;
            }
            string path = item.folderItems.Path;
            Microsoft.MediaCenter.UI.Application.DeferredInvokeOnWorkerThread(AddSong, done, path);
        }

        private void AddSong(object obj)
        {
            ThreadPriority priority = Thread.CurrentThread.Priority;
            Thread.CurrentThread.Priority = ThreadPriority.Lowest;

            string path = Convert.ToString(obj);

            try
            {

               foreach (string fileName in Directory.GetFiles(path, "*.*"))
                {
                    if (Helper.IsSong(fileName))
                    {
                        Microsoft.MediaCenter.Hosting.AddInHost.Current.MediaCenterEnvironment.PlayMedia(MediaType.Audio, fileName, true); 
                        
                    }

                }
                // Add all directories at this directory.
                string[] subdirEntries = Directory.GetDirectories(path);
                foreach (string directoryName in subdirEntries)
                {

                    try
                    {
                        DirectoryInfo info = new DirectoryInfo(directoryName);
                        if ((info.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
                            AddSong(directoryName);

                    }
                    catch (UnauthorizedAccessException) { }
                }


            }
            finally
            {
               
                Thread.CurrentThread.Priority = priority;
            }
        }

        private void done(object obj)
        {
            Microsoft.MediaCenter.Hosting.AddInHost.Current.MediaCenterEnvironment.MediaExperience.GoToFullScreen();
        }



        public void FullScreen()
        {
            MediaCenterEnvironment mce;
            mce = Microsoft.MediaCenter.Hosting.AddInHost.Current.MediaCenterEnvironment;

            if (mce.MediaExperience != null) mce.MediaExperience.GoToFullScreen();
        }

        public void PlaySong(string path, Boolean add)
        {
            MediaCenterEnvironment mce;
            mce = Microsoft.MediaCenter.Hosting.AddInHost.Current.MediaCenterEnvironment;
            if (mce.MediaExperience == null && add == true) add = false;
            mce.PlayMedia(MediaType.Audio, path, add);
            mce.MediaExperience.GoToFullScreen();
        }


       
    }

  
}